<template>
	<view class="sport-content">
		<certificate @goApplication="goApplication" v-if="sport_user.code&&sport_user.code.length>0" :details="certificateInfo"></certificate>
		<view v-else-if="sport_user.id&&sport_user.id!=undefined">
			<view class="sport-header">
				<view class="sport-header_upload" v-if="sport_user.is_finish&&sport_user.is_finish!='1'">
					<image class="sport-header_upload_icon" src="../../static/imgs/mine/sucess.png"></image>
					<view class="sport-header_upload_title text_black">恭喜您，已完成全部打卡</view>
					<view class="sport-header_upload_tips text_opblack">正在为您评分，请您耐心等待</view>
				</view>
				<image v-else :src="sport.thumb"></image>
			</view>
			<view class="sport-search">
				<view class="sport-search-input">
					<view class="text_black">参赛人姓名</view>
					<view class="application-user text_black">{{sport_user.name}}</view>
				</view>
				<view class="sport-search-input">
					<view class="text_black">选择阶段</view>
					<view class="application-user text_black">
						<text v-for="(item,index) in stageList" :key="index"
							v-if="item.section_id==sport_user.section_id">
							{{item.name}}
						</text>
					</view>
				</view>
			</view>
			<view class="text_opblack application-tips">您已完成报名，请开始训练，点击下方去打卡</view>
			<l-collapse v-if="competition.length>0">
				<l-collapse-item v-for="(item,index) in competition" :key="index" :title="item.title" :rightIcon="false" :tips="item.sub_title" :open="item.user_courses.length>0">
					<!-- :planImg="item.thumb" :planTitle="item.sub_title" -->
					<view class="content">
						<view class="planItem" v-for="(pitem,pindex) in item.user_courses" :key="pindex"
							@click="goDetails(pitem,item)">
							<view class="planItem_name text_black">{{pitem.title}}</view>
							<view class="planItem_states">
								<view :class="pitem.user_video&&pitem.user_video.length>0?'text_active':'text_opblack'">
									{{pitem.user_video&&pitem.user_video.length>0?'已打卡':'点击打卡'}}
								</view>
								<image src="../../static/imgs/mine/right.png"></image>
							</view>
						</view>
					</view>
				</l-collapse-item>
			</l-collapse>
			<view v-else class="text_opblack noList">暂无内容~</view>
		</view>
		<view v-else class="application-bg">
			<image class="application-img" src="https://qiniu.aobeitineng.com/sport/sport_hd.png" mode="widthFix"></image>
			<view class="sport-search">
				<view class="sport-search-input">
					<view class="text_black">参赛人姓名</view>
					<input v-model="search.name" placeholder="请填写参赛人姓名" />
				</view>
				<view class="sport-search-input">
					<view class="text_black">参赛阶段</view>
					<picker @change="bindPickerChange" :value="stageIndex" :range="stageList" range-key="name">
						<view class="placeholder" v-if="stageIndex==null">
							请选择
							<image src="../../static/imgs/mine/botton_icon.png" class="bottomIcon"></image>
						</view>
						<view class="uni-input" v-else>
							{{stageList[stageIndex].name}}
							<image src="../../static/imgs/mine/botton_icon.png" class="bottomIcon"></image>
						</view>
					</picker>
				</view>
				<view class="button" @click="save">
					<image mode="widthFix" src="../../static/imgs/games/footer-btn.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSportIndex,
		getSportEnter,
		getSportSection
	} from "../../static/js/api/games.js";
	import lCollapse from "../../components/l-collapse/l-collapse.vue"
	import lCollapseItem from "../../components/l-collapse-item/l-collapse-item.vue";
	import certificate from "../../components/certificate/certificate.vue";
	export default {
		components: {
			lCollapse,
			lCollapseItem,
			certificate
		},
		data() {
			return {
				loading: false,
				search: {
					stage: 1,
					name: '',
				},
				stageList: [],
				sport: {},
				stageIndex: null,
				is_application: 1, //0未报名 1已报名
				competition: [],
				sport_user: {},
				certificateInfo:{},
			}
		},
		onShow() {
			this.getData()

		},
		methods: {
			// 获取报名页信息
			getData() {
				let _this = this
				getSportIndex().then(res => {
					console.log(res)
					_this.stageList = res.sections
					_this.sport = res.sport
					_this.sport_user = res.sport_user
					let timer = setInterval(function(){
						_this.competition = res.details
						clearInterval(timer)
					},200)
					if(_this.sport_user.code&&_this.sport_user.code.length>0){
						_this.certificateInfo = _this.sport_user
						_this.certificateInfo.sections = res.sections
					}
					_this.$forceUpdate()
					
				})
			},
			// 选择阶段
			bindPickerChange(e) {
				console.log(e.detail.value)
				this.stageIndex = e.detail.value
				this.search.stage = this.stageList[this.stageIndex].section_id
			},
			// 课程打卡
			goDetails(item, parentInfo) {
				item.sub_title = parentInfo.sub_title
				item.section_id = parentInfo.section_id
				item.sport_user_id = this.sport_user.id
				let details = JSON.stringify(item)
				uni.navigateTo({
					url: "/pages/games/details?details=" + details
				})
			},
			// 报名
			save() {
				let _this = this
				if (_this.search.name.length == 0) {
					uni.showToast({
						title: "请填写报名信息",
						icon: "none"
					})
					return
				}
				if (_this.search.stage.length == 0) {
					uni.showToast({
						title: "请填写报名信息",
						icon: "none"
					})
					return
				}
				getSportEnter({
					sport_id: _this.sport.id,
					section_id: _this.search.stage,
					name: _this.search.name
				}).then(res => {
					uni.showToast({
						icon: 'none',
						title: "恭喜您，报名成功，开始训练打卡吧！"
					})
					this.getData()
				})
			},
			//获取运动会的课程
			// getSection(){
			// 	getSportSection({
			// 		sport_id:this.sport.id,
			// 		section_id:this.search.stage,
			// 	}).then(res=>{
			// 		console.log(156,res)
			// 		this.competition = res.lists
			// 	})
			// },
			goApplication(){
				this.sport_user = {}
				this.competition = []
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import url("./index.scss");
	.application-bg{
		width: 100%;
		padding-bottom: 492rpx;
		.application-img{
			width: 100%;
		}
		.sport-search{
			position: fixed;
			bottom: 0;
			z-index: 999;
			height: 492rpx;
			width: 100%;
			height: 436rpx;
			background-image: url('../../static/imgs/games/footer-bg.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-top: 56rpx;
			.sport-search-input{
				width: 630rpx;
				height: 100rpx;
				border-radius: 10rpx;
				margin-bottom: 30rpx;
			}
		}
		.button{
			background-color: transparent;
			image{
				width: 690rpx;
				height: 110rpx;
			}
		} 
	}
	
	.sport-content /deep/ .l-collapse-item__title-box {
		padding: 0;
	}

	.sport-content /deep/ .l-collapse-item__title-text {
		padding: 0 0 0 30rpx;
		border-left: 12rpx solid $uni-color-primary;
	}
	.sport-content /deep/ .l-collapse-item__title-tips{
		font-size: 24rpx;
		line-height: 34rpx;
		margin-left: 20rpx;
	}
	.sport-content /deep/ .l-collapse-item--animation{
		display: none;
	}
	.text_opblack {
		color: $uni-text-color-opblack;
	}

	.text_black {
		color: $uni-text-color-black;
	}

	.text_active {
		color: $uni-color-primary;
	}

	
	.games-tips{
		margin-top: 20rpx;
	}
	
	.noList{
		text-align: center;
		padding-top: 60rpx;
	}
</style>